<?php
declare (strict_types = 1);

namespace app;

use app\model\Classify;
use app\model\ConfigModel;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\View;
/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {


        $catid = input('catid', 0);
        View::assign('catid', $catid);


        // 获取当前时间
        $now = date('Y年m月d日');
        View::assign('now', $now);

        // 获取今天是周几
        $weekarray = array("日", "一", "二", "三", "四", "五", "六");
        $week = "星期" . $weekarray[date("w")];
        View::assign('week', $week);

        // 获取网站配置 根据group 分组
        $config = ConfigModel::field('id,name,title,content,type')->where(['deleted' => 0,'tenant_id' => 1])->select();
        foreach ($config as $key => $value) {
            if ($value['type'] == 'images') {
                $value['content'] = explode(',', $value['content']);
            }
        }
        View::assign('config', $config);
        // 获取网站配置 SEO信息 $config中 web_site_title web_site_keyword web_site_description
        $seo = [];

        foreach ($config as $key => $value) {
            if ($value['name'] == 'web_site_title') {
                $seo['title'] = $value['content'];
            }
            if ($value['name'] == 'web_site_keyword') {
                $seo['keyword'] = $value['content'];
            }
            if ($value['name'] == 'web_site_description') {
                $seo['description'] = $value['content'];
            }
        }
        View::assign('SEO', $seo);


       $industry = Classify::where(['deleted' => 0,'status_flag' => 0])->order("listorder")->select();
        // 返回页面
       View::assign('industry', $industry);


    }


    function isMobile()
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        $mobileKeywords = ['Mobile', 'Android', 'iPhone', 'iPad', 'Windows Phone'];

        foreach ($mobileKeywords as $keyword) {
            if (strpos($userAgent, $keyword) !== false) {
                return true;
            }
        }
        return false;
    }


/**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

}
